<template>
  <div class="mod-config">
    <basic-container>
      <el-form :inline="true" v-show="searchShow" ref="searchForm" :model="dataForm" @keyup.enter.native="searchHandle">
        <el-form-item label="商品类别" prop="ctgId">
          <el-select v-model="dataForm.ctgId" @change="getSupplyList">
            <el-option v-for="item in ctgIdOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="制造商">
          <el-input v-model="dataForm.supplyName" :disabled="disable">
            <el-button slot="append" @click="openDialog('supply')" v-if="!dataForm.id">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="商品品种">
          <el-input v-model="dataForm.brandName" :disabled="disable">
            <el-button slot="append" @click="openDialog('brand')" v-if="!dataForm.id">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="商品名称" prop="viewGoodsName">
          <el-input v-model="dataForm.viewGoodsName"></el-input>
        </el-form-item>
        <el-form-item label="商品编码" prop="code">
          <el-input v-model="dataForm.goodsCode"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="24">
          <el-button v-if="permissions.eims_maxdiscount_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button icon="el-icon-plus" type="primary" @click="batchUpdate()">批量修改</el-button>
          <el-button icon="el-icon-plus" type="primary" @click="batchDelete()" :loading="isDelete">批量删除</el-button>
          <el-button icon="el-icon-search" circle @click="handleSearchShow" style="float:right;"></el-button>
          <el-button icon="el-icon-refresh" circle @click="refreshChange" style="float:right;"></el-button>
        </el-col>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button v-if="permissions.eims_maxdiscount_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
              <el-button v-if="permissions.eims_maxdiscount_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="ctgName" label="商品类别"></el-table-column>
          <el-table-column prop="supplyName" label="制造商"></el-table-column>
          <el-table-column prop="brandName" label="商品品种"></el-table-column>
          <el-table-column prop="goodsCode" label="商品编码"></el-table-column>
          <el-table-column prop="viewGoodsName" label="商品名称"></el-table-column>
          <el-table-column prop="maxRate" label="最大折扣率(0-1)"></el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      <!-- 弹窗, 批量修改 -->
      <batch-info v-if="batchInfoVisible" ref="batchInfo" @refreshDataList="getDataList"></batch-info>
      <!-- 商品制造商信息 -->
      <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
      <!-- 商品品种信息 -->
      <search-brand v-if="brandVisible" ref="brandDialog" @refreshData="getBrandData"></search-brand>
    </basic-container>
  </div>
</template>

<script>
  import { filterParams } from '@/util/tools'
  import { fetchList, delObj, batchRemove } from "@/api/eims/maxdiscount";
  import TableForm from "./maxdiscount-form";
  import BatchInfo from "./batchInfo";
  import { mapGetters } from "vuex";
  import { fetchCtgSaleList } from "@/api/eims/ctg";
  import SearchSupply from '@/components/business/SearchSupply' //商品制造商
  import SearchBrand from '@/components/business/SearchBrand' //商品品种


  export default {
    data() {
      return {
        isDelete: false,
        dataForm: {
          supplyId: '',
          supplyName: '',
          brandId: '',
          brandName: '',
        },
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        batchInfoVisible: false,
        supplyVisible: false,
        brandVisible: false,
        ids: [],
        multipleSelection: [],
        ctgIdOptions: [],
        supplyOptions: [],
        brandOptions: [],
      };
    },
    components: {
      TableForm,
      BatchInfo,
      SearchSupply,
      SearchBrand,
    },
    created() {
      this.getDataList();
      this.getCtgList();
    },
    computed: {
      ...mapGetters(["permissions"])
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = filterParams(this.dataForm)
        this.dataListLoading = true;
        fetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        });

      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      },
      // 删除
      deleteHandle(id) {
        this.$confirm("是否确认删除ID为" + id, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delObj(id);
          }).then(response => {
            if(response.data.code == '0'){
              this.$message.success("删除成功")
              this.getDataList()
            }else{
              this.$message.error(response.data.msg)
            }
          })
      },
      searchChange() {
        this.pageIndex = 1;
        this.getDataList();
      },
      // 搜索清空
      searchReset() {
        this.supplyOptions = []
        this.brandOptions = []
        for (var key in this.dataForm) {
          this.dataForm[key] = ''
        }
        this.$refs.searchForm.resetFields();
        this.getDataList(this.page);
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page);
      },
      handleSelectionChange(val) {
        let list = []
        this.multipleSelection = val;
        val.forEach(element => {
          list.push(element.id)
        });
        this.ids = list;
      },
      //批量修改
      batchUpdate() {
        if (!this.ids.length) {
          this.$message.warning('请选择修改对象')
          return
        }
        this.batchInfoVisible = true;
        this.$nextTick(() => {
          this.$refs.batchInfo.init(this.ids);
        });
      },
      //批量删除
      batchDelete() {
        this.isDelete = true
        if (!this.ids.length) {
          this.isDelete = false
          this.$message.warning('请选择删除对象')
          return
        }
        batchRemove(this.ids).then(response => {
          if(response.data.code == '0'){
            this.isDelete = false
            this.$message.success("批量删除成功")
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        }).catch(() => {
          this.isDelete = false
        })
      },
      getCtgList() {
        fetchCtgSaleList({type: 3}).then(response => {
          if(response.data.code == '0'){
            let result = response.data.data
            let list = []
            result.forEach(item => {
              list.push({
                value: item.id,
                label: item.name
              })
            })
            this.ctgIdOptions = list
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  打开弹窗
      openDialog(name) {
        if (name == 'supply') {
          if (!this.dataForm.ctgId) {
            this.$message.warning('请先选择商品类别')
            return
          }
          let data = {
            ctgId: this.dataForm.ctgId
          }
          this.supplyVisible = true
          this.$nextTick(() => {
            this.$refs.supplyDialog.init(data)
          })
        } else if (name == 'brand') {
          if (!this.dataForm.supplyId) {
            this.$message.warning('请先选择所属制造商')
            return
          }
          let data = {
            ctgId: this.dataForm.ctgId,
            supplyId: this.dataForm.supplyId
          }
          this.brandVisible = true
          this.$nextTick(() => {
            this.$refs.brandDialog.init(data)
          })
        }
      },

      //  获取制造商数据
      getSupplyData(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
      },
      //  获取商品品种信息
      getBrandData(data) {
        this.dataForm.brandId = data.id
        this.dataForm.brandName = data.name
      },
    }
  };
</script>
